package com.app.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;


/**
 * @author YangHua 转载请注明出处：http://www.xfok.net/2009/10/124485.html
 */
public class SftpUtil {
	private JSch jsch = new JSch();
	private Session sshSession;
	private ChannelSftp sftp;
	private static Log log = LogFactory.getLog(SftpUtil.class);

	/**
	 * 连接sftp服务器
	 * @param host主机
	 * @param port端口
	 * @param username用户名
	 * @param password密码
	 * @throws JSchException 
	 */
	public void connect(String host, String username, String password) throws JSchException {
		this.connect(host, 22, username, password);
	}

	public void connect(String host, int port, String username, String password) throws JSchException {
			sshSession = jsch.getSession(username, host, port);
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			sshSession.connect();
			Channel channel = sshSession.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
	}

	/**
	 * 上传文件
	 * @param directory上传的目录
	 * @param uploadFile要上传的文件
	 * @param sftp
	 */
	public void upload(String directory, String uploadFile) {
		try {
			sftp.cd(directory);
			File file = new File(uploadFile);
			sftp.put(new FileInputStream(file), file.getName());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	/**
	 * 下载文件
	 * @param directory下载目录
	 * @param downloadFile下载的文件
	 * @param saveFile存在本地的路径
	 * @throws SftpException 
	 * @throws FileNotFoundException 
	 */
	public void download(String directory, String downloadFile, String saveFile) throws Exception {
		
			sftp.cd(directory);
			File file = new File(saveFile);
			file.getParentFile().mkdirs();
			sftp.get(downloadFile, new FileOutputStream(file));
		
	}

	/**
	 * 删除文件
	 * @param directory要删除文件所在目录
	 * @param deleteFile要删除的文件
	 * @param sftp
	 */
	public void delete(String directory, String deleteFile) {
		try {
			sftp.cd(directory);
			sftp.rm(deleteFile);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}

	
	/**
	 * 列出目录下的文件
	 * @param directory要列出的目录
	 * @param sftp
	 * @throws SftpException
	 */
	public Vector listFiles(String directory) throws SftpException {
		return sftp.ls(directory);
	}

	public void disconnect() {
		try {
			this.sftp.disconnect();
			this.sftp.exit();
			this.sshSession.disconnect();
		} catch (Exception e) {

		}
	}
	
	public static void main(String[] args) throws Exception {
		/*SftpUtil sf = new SftpUtil();
		String host = "192.16.73.230";
		String username = "root";
		String password = "1qaz2wsx";
		sf.connect(host, username, password);
		SSHUtil ssh = new SSHUtil();
		ssh.connect(host, 22, username,password);
		
		long start = System.currentTimeMillis();
		sf.upload("/data/zsl/test", "E:/浙江运维/project.log");
		long end = System.currentTimeMillis();
		System.out.println("-------------sql upload:"+(end-start));
		ssh.execCommand("rm -rf /data/zsl/test/project.log");
		///////////////////////
		
		ZipUtil.makeFileZip("E:/浙江运维/project.log","E:/浙江运维/project.zip");
		long end2 = System.currentTimeMillis();
		System.out.println("ZipUtil zip:"+(end2-end));
		
		sf.upload("/data/zsl/test", "E:/浙江运维/project.zip");
		long end3 = System.currentTimeMillis();
		System.out.println("upload zip:"+(end3-end2));
		sf.disconnect();
		
		String path = "unzip /data/zsl/test/project.zip -d /data/zsl/test/" ;
		ssh.execCommand(path);
		ssh.disconnect();
		long end4 = System.currentTimeMillis();
		System.out.println("unzip zip:"+(end4-end3));

		System.out.println("----------------zip upload:"+(end4-end));*/
	}
}
